package com.mengfly.ai.testcroseplatformproject

import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.datetime.LocalTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import org.jetbrains.compose.ui.tooling.preview.Preview
import kotlin.time.Clock
import kotlin.time.ExperimentalTime

@Composable
@Preview
fun App2() {
    MaterialTheme {
        var showCountry by remember { mutableStateOf(false) }
        var timeAtLocation by remember { mutableStateOf("No location selected") }

        Column(
            modifier = Modifier
                .padding(20.dp)
                .safeContentPadding()
                .fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                timeAtLocation,
                style = TextStyle(fontSize = 20.sp),
                textAlign = TextAlign.Center,
                modifier = Modifier.fillMaxWidth().align(Alignment.CenterHorizontally)
            )
            Row(modifier = Modifier.padding(start = 20.dp, top = 10.dp)) {

                DropdownMenu(
                    expanded = showCountry,
                    onDismissRequest = { showCountry = false }
                ) {
                    countries.forEach { (name, zone) ->
                        DropdownMenuItem(
                            text = { Text(name) },
                            onClick = {
                                showCountry = false
                                timeAtLocation = currentTimeAt(name, zone)
                            }
                        )
                    }
                }
            }
            Button(
                onClick = { showCountry = !showCountry },
                modifier = Modifier.padding(top = 10.dp)
            ) {
                Text("Selectn Location")
            }
        }

    }
}

@OptIn(ExperimentalTime::class)
fun currentTimeAt(location: String, zone: TimeZone): String {
    fun LocalTime.formatted() = "$hour:$minute:$second"

    val time = Clock.System.now()
    val localTime = time.toLocalDateTime(zone).time

    return "The time in $location is ${localTime.formatted()}"
}

data class Country(val name: String, val zone: TimeZone)

val countries = listOf(
    Country("China", TimeZone.of("Asia/Shanghai")),
    Country("Afghanistan", TimeZone.of("Asia/Kabul")),
    Country("Albania", TimeZone.of("Europe/Tirane")),
    Country("Algeria", TimeZone.of("Africa/Algiers")),
    Country("Andorra", TimeZone.of("Europe/Andorra")),
    Country("Angola", TimeZone.of("Africa/Luanda")),
    Country("Antigua and Barbuda", TimeZone.of("America/Antigua")),
    Country("Argentina", TimeZone.of("America/Argentina/Buenos_Aires")),
)